<?php
/**
 * Task.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-04-14 15:31:59
 * @modified    2023-04-14 15:31:59
 *
 */

namespace App\Models\Project;

use App\Models\BaseModel;
use App\Models\Project;
use App\Models\Project\Task\Product;
use App\Models\Project\Task\User;
use App\Repositories\TaskRepo;
use Carbon\Carbon;

class Task extends BaseModel
{
    protected $table = 'task';
    protected $primaryKey = 'id';

    public const CREATED_AT = 'create_time';
    public const UPDATED_AT = 'update_time';

    protected $fillable = [
        'task_number',
        'name',
        'project_id',
        'plan_product_quantity',
        'type',
        'plan_start_at',
        'start_at',
        'plan_complete_at',
        'complete_at',
        'percent',
        'plan_work_hour',
        'work_hour',
        'description',
        'status',
        'create_user_id',
        'mark',
        'close_reason'
    ];

    protected $appends = [
        'percent_format',
        'status_text',
        'schedule',
        'schedule_text'
    ];

    public function getScheduleTextAttribute()
    {
        $schedule = $this->schedule;
        if ($schedule == '') {
            return '--';
        }
        $scheduleStatus = collect(\App\Repositories\TaskRepo::schedule_status())->keyBy('code')->toArray();
        $currentScheduleStatus = $scheduleStatus[$schedule] ?? [];
        return $currentScheduleStatus['title'] ?? '';
    }

    public function getScheduleAttribute()
    {
        $planStartAt = $this->plan_start_at;
        $completeDate = $this->complete_at;
        $planCompleteAt = $this->plan_complete_at;
        $now = Carbon::now()->toDateString();
        $status = $this->status;

        if ($status == \App\Repositories\TaskRepo::STATUS_UN_START) {
            if ($planStartAt < $now) {
                return \App\Repositories\TaskRepo::SCHEDULE_STATUS_DELAY;
            } else {
                return \App\Repositories\TaskRepo::SCHEDULE_STATUS_UN_START;
            }
        }
        if ($status == \App\Repositories\TaskRepo::STATUS_STARTED) {
            if ($planCompleteAt < $now) {
                return \App\Repositories\TaskRepo::SCHEDULE_STATUS_POSTPONE;
            } else {
                return \App\Repositories\TaskRepo::SCHEDULE_STATUS_PROCESSING;
            }
        }
        if ($completeDate < $planCompleteAt) {
            return \App\Repositories\TaskRepo::SCHEDULE_STATUS_COMPLETE_BEFORE;
        } elseif ($completeDate == $planCompleteAt) {
            return \App\Repositories\TaskRepo::SCHEDULE_STATUS_COMPLETE_EQUAL;
        } elseif ($completeDate > $planCompleteAt) {
            return \App\Repositories\TaskRepo::SCHEDULE_STATUS_COMPLETE_AFTER;
        }
        return '';
    }

    public function getPercentFormatAttribute()
    {
        return round($this->percent, 2) . '%';
    }

    public function getStatusTextAttribute()
    {
        $status = $this->status;
        $statuses = collect(\App\Repositories\TaskRepo::status())->keyBy('code')->toArray();
        $currentStatus = $statuses[$status] ?? [];
        return $currentStatus['title'] ?? '';
    }

    public function scopeByNumber($query, $number)
    {
        return $query->where('task_number', $number);
    }

    public function users()
    {
        return $this->hasMany(User::class);
    }

    public function histories()
    {
        return $this->hasMany(Project\Task\History::class)->orderBy('create_time');
    }

    public function products()
    {
        return $this->hasMany(Product::class);
    }

    public function project()
    {
        return $this->belongsTo(Project::class);
    }
}